/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.farmcode.web.generator.match;

import com.farmcode.console.api.business.custom.ExtMatchManager;
import com.farmcode.console.api.model.Match;
import com.farmcode.console.api.model.Project;
import com.farmcode.console.api.model.Template;
import com.farmcode.web.generator.BaseCodeGenerator;
import java.util.List;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;

/**
 *
 * @author Oscar Ivan Prado
 */
@Named(value = "matchBackingBean")
@RequestScoped
public class MatchBackingBean extends BaseCodeGenerator {

    private List<Match> matchs;
    private Match match;
    private Match newMatch;
    private Long templateId;
    @EJB
    private ExtMatchManager extMatchManager;

    /** Creates a new instance of Match */
    public MatchBackingBean() {
    }

    /**
     * @return the matchs
     */
    public List<Match> getMatchs() {
        setMatchs(null);
        if (getSession().getValue(getClass().getCanonicalName() + ".matchs") != null) {
            matchs = getSession().getValue(getClass().getCanonicalName() + ".matchs");
        } else {
            Project project = getProject();
            Template template = getSession().getValue(new Template().getClass().getCanonicalName());
            if (project != null && project.getId() != null
                    && template != null && template.getId() != null) {
                setMatchs(extMatchManager.getMatchByProject(getProject(), template));
            }
        }
        return matchs;
    }

    public void onSave() {
        try {
            Match tmpMatch = getNewMatch();
            Template template = getSession().getValue(new Template().getClass().getCanonicalName());
            tmpMatch.setProject(getProject());
            tmpMatch.setTemplate(template);
            tmpMatch.setRelocate("N");
            tmpMatch.setActive("Y");
            extMatchManager.create(tmpMatch);
            setNewMatch(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void onRemove(Match removeMatch) {
        try {
            extMatchManager.remove(removeMatch);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void onModify() {
        try {
            extMatchManager.store(getMatch());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void openMatchContext(Match match) {
        System.out.println(match.getId());
        setMatch(match);
    }

    /**
     * @param matchs the matchs to set
     */
    public void setMatchs(List<Match> matchs) {
        this.matchs = (matchs);
        getSession().setValue(getClass().getCanonicalName() + ".matchs", matchs);
    }

    /**
     * @return the match
     */
    public Match getMatch() {
        match = new Match();
        if (getSession().getValue(match.getClass().getCanonicalName()) != null) {
            match = getSession().getValue(match.getClass().getCanonicalName());
        }
        return match;
    }

    /**
     * @param match the match to set
     */
    public void setMatch(Match match) {
        this.match = match;
        getSession().setValue(new Match().getClass().getCanonicalName(), match);
    }

    /**
     * @return the newMatch
     */
    public Match getNewMatch() {
        newMatch = new Match();
        if (getSession().getValue(new Match().getClass().getCanonicalName() + "_new") != null) {
            newMatch = getSession().getValue(new Match().getClass().getCanonicalName() + "_new");
        } else {
            setNewMatch(newMatch);
        }
        return newMatch;
    }

    /**
     * @param newMatch the newMatch to set
     */
    public void setNewMatch(Match newMatch) {
        this.newMatch = newMatch;
        getSession().setValue(new Match().getClass().getCanonicalName() + "_new", newMatch);
    }

    /**
     * @return the templateId
     */
    public Long getTemplateId() {
        if (getSession().getValue("templateId") != null) {
            templateId = getSession().getValue("templateId");
        }
        return templateId;
    }

    /**
     * @param templateId the templateId to set
     */
    public void setTemplateId(Long templateId) {
        this.templateId = templateId;
        getSession().setValue("templateId", templateId);
    }
}
